<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\ImageModel;

class Image extends Base
{
    // 图片列表
    public function index()
    {
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $image = new imageModel();
            $where = [];
            $selectResult = $image->getimagesByWhere($where, $offset, $limit);
            foreach($selectResult as $key => $vo){
                 $selectResult[$key]['pic'] = '<img src="' . $vo['pic'] . '" width="40px" height="40px">';
                 $selectResult[$key]['is_put'] = ($vo['is_put'] == '0')?'下架':'上架';
                 $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }
            $return['total'] = $image->getAllimages($where);  // 总数据
            $return['rows'] = $selectResult;
            return json($return);
        }

        return $this->fetch();
    }

    // 添加文章
    public function imageadd()
    {
        if(request()->isPost()){
            $param = input('post.');
            unset($param['file']);
            $param['addtime'] = date('Y-m-d H:i:s');
            $image = new imageModel();
            $flag = $image->addimage($param,url('image/index'));
            if($flag['code'] == '1'){
               $this->success('添加成功','index');
            }
        }
    }

    public function imageDel()
    {
        $id = input('param.id');
        $image = new imageModel();
        $pic = $image->getOnefield(['id' => $id],'pic');
        unset($pic);
        $flag = $image->delimage($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }
    function uploadimg_del(){
        $path = input('param.pic');
        unset($path);
    }
    public function imageisput()
    {
        $id = input('param.id');
        $is_put = input('param.is_put');
        $image = new imageModel();
        $flag = $image->image_is_put(['is_put' => $is_put],['id' => $id]);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    // 上传缩略图
    public function uploadimg()
    {
        if(request()->isAjax()){
            $file = request()->file('file');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload'.DS.'image');
            if($info){
                $src =  '/upload'.'/image' . '/' . date('Ymd') . '/' . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }
    // 音频文件
    public function uploadmusic()
    {
        if(request()->isAjax()){
            $file = request()->file('file');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload'.DS.'music');
            if($info){
                $src =  '/upload/music' . '/' . date('Ymd') . '/' . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        $image = new imageModel();
        $is_put = $image->getOnefield(['id' => $id],'is_put');
        $return_data = [
            '删除' => [
                'auth' => 'image/imagedel',
                'href' => "javascript:imageDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ],
        ];
        if(!$is_put){
            $return_data['上架'] =  [
                'auth' => 'image/imageedit',
                'href' => "javascript:imageisput(".$id.",1".")",
                'btnStyle' => 'primary',
                'icon' => ''
            ];
        }else{
            $return_data['下架'] =  [
                'auth' => 'image/imageedit',
                'href' => "javascript:imageisput(".$id.",0".")",
                'btnStyle' => 'primary',
                'icon' => ''
            ];
        }
        return $return_data;

    }
}
